// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import { getCookie, backToHost, platform } from './utils'
import './assets/styles/reset.css'
import './assets/styles/font.css'
import PopUp from 'vc-popup'
import 'vc-popup/lib/style.css'
// 引入高德地图
import VueAMap from 'vue-amap';

Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
  key: '4f99df29c6bfc721a8fb3e96364b57fa',
  plugin: ['AMap.Geolocation','AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
  // 默认高德 sdk 版本为 1.4.4
  v: '1.4.4',
  uiVersion: '1.0.11' // 版本号
});

Vue.use(PopUp)
Vue.config.productionTip = false
window.rcsClientReady = false
/* eslint-disable no-new */
// 等待和飞信客户端注入H5 API完成后再初始化界面
window.rcsOptimizeReady = function () {
  // 在安卓平台下每次window.location的hash变化都会触发一次window.rcsOptimizeReady调用
  // 导致vue会多次实例化,所以需要自己需要判断是否为第一次window.rcsOptimizeReady调用
  if (window.rcsClientReady) {
    return false
  }

  window.rcsClientReady = true
  /* eslint-disable no-new */
  window.RcsVueRoot = new Vue({
    el: '#app',
    data () {
      return {
        ENV: ({'121.15.167.235': 'develop'})[window.location.hostname] || 'production',
        'bmap-ak': 'VMKSqf62rAUZ17BWAhIi8qW06RhBdTwy',
        platformClass: {
          topArticle: platform.is('iPhone') ? 'wrap' : 'defaultWrap'
        },
        isAdmin: true || getCookie('status') === '1',
        isIphone: platform.is('iPhone'),
        userInfo: {
          address: '',
          birthday: '',
          company: '',
          department: '',
          email: '',
          enterpriseId: getCookie('enterId'),
          headColor: '',
          headImg: '',
          id: getCookie('uid'),
          name: '',
          phone: '',
          position: '',
          isAppealApprover: getCookie('examinerState') === '1'
        },
        myGroupInfo: {
          enterId: '',
          id: '',
          employeeName: '',
          phone: '',
          employeeId: '',
          status: '',
          attendanceId: '',
          attendanceName: '',
          longitude: '',
          latitude: '',
          range: '',
          outRange: '0',
          location: '',
          detailAddr: '',
          examineName: '',
          examineUid: ''
        },
        newGroupName: '',
        newGroupUsers: [],
        newGroupPosition: {
          location: '',
          detailAddr: '',
          longitude: '',
          latitude: ''
        },
        tipShow: false,
        tip: '',
        confirmShow: false,
        confirmMsg: '',
        confirmContext: null,
        confirmCb1: null,
        confirmCb2: null,
        appealRecord: {
          'monthRcdId': '',
          'attendanceDate': '',
          'goWork': '',
          'goWorkDesc': '',
          'leaveWork': '',
          'leaveWorkDesc': '',
          'remark': '',
          'recordState': '1'
        }
      }
    },
    router,
    template: '<App :tip="tip" :tipShow="tipShow" @toggleTip="_toggleTip" :confirmMsg="confirmMsg" :confirmShow="confirmShow" @confirmCancel="_confirmCancel" @confirmOk="_confirmOk"/>',
    components: { App },
    methods: {
      backToHost () {
        backToHost()
      },
      _toggleTip () {
        this.tipShow = !this.tipShow
      },
      _showTip (tip) {
        this.tip = tip
        this.tipShow = true
      },
      _confirmCancel () {
        this.confirmShow = false
        try {
          if (this.confirmCb1) {
            this.confirmCb1.call(this.confirmContext)
          }
        } catch (e) {
          console.log(e)
        }
        this.confirmContext = null
        return false
      },
      _confirmOk () {
        this.confirmShow = false
        try {
          if (this.confirmCb2) {
            this.confirmCb2.call(this.confirmContext)
          }
        } catch (e) {
          console.log(e)
        }
        this.confirmContext = null
        return true
      },
      _showConfirm (msg, cb1, cb2, context) {
        if (this.confirmContext) {
          // 保证同一时间只出现一个confirm对话框
          return false
        }
        this.confirmMsg = msg
        this.confirmShow = true
        this.confirmContext = context
        this.confirmCb1 = cb1
        this.confirmCb2 = cb2
      },
      addLog (msg) {
        console.log(typeof msg === 'object' ? JSON.stringify(msg) : msg)
      },
      setUserInfo (userInfo) {
        Object.assign(this.userInfo, userInfo)
        this.$emit('setUserInfo', userInfo)
      },
      setMyGroupInfo (groupInfo) {
        this.myGroupInfo = groupInfo
        this.$emit('setMyGroupInfo', groupInfo)
      },
      setNewGroupName (groupName) {
        this.newGroupName = groupName
        this.$emit('setNewGroupName', groupName)
      },
      setNewGroupUsers (users) {
        this.newGruopUsers = users
        this.$emit('setNewGroupUsers', users)
      },
      setNewGroupPosition (position) {
        this.newGroupPosition = position
        this.$emit('setNewGroupPosition', position)
      },
      resetNewGroupInfo () {
        this.newGroupName = ''
        this.newGroupUsers = []
        this.newGroupPosition = {
          location: '',
          detailAddr: '',
          longitude: '0',
          latitude: '0'
        }
      },
      setAppealRecord (record) {
        this.appealRecord = record
      },
      resetAppealRecord () {
        this.appealRecord = {
          'monthRcdId': '',
          'attendanceDate': '',
          'goWork': '',
          'goWorkDesc': '',
          'leaveWork': '',
          'leaveWorkDesc': '',
          'remark': '',
          'recordState': '1'
        }
      }
    }
  })
}

// 在pc上调试时触发
// if (platform.is('Desktop')) {
//   window.rcsOptimizeReady()
// }
window.rcsOptimizeReady()
